package com.example.demo.service.serviceImpl;

import com.example.demo.entity.BackResponseEntity;
import com.example.demo.entity.LeveltwoLabel;
import com.example.demo.entity.requestJson.AInsertLabelReq;
import com.example.demo.entity.responseJson.AQueryLabelRes;
import com.example.demo.entity.responseJson.ListResult;
import com.example.demo.enums.ErrCodeEnum;
import com.example.demo.mapper.ALabelMapper;
import com.example.demo.mapper.LeveltwoLabelMapper;
import com.example.demo.service.ALabelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @author : 杨丽辉
 * @date : 2021/3/1 14:37
 * @description:
 */

@Service
@Transactional(rollbackFor = {RuntimeException.class, Exception.class})
public class ALabelServiceImpl implements ALabelService {
    @Autowired
    private LeveltwoLabelMapper leveltwoLabelMapper;
    @Autowired
    private ALabelMapper aLabelMapper;

    /*
     * (在添加标签页面：作者可以通过添加标签按钮，自己添加标签，也可以通过后端返回的热度标签中选择)
     * （在热度标签选择的删除不需要通过后端，直接前端点×通过前端参数使其不在选择列表出现）
     * （作者添加标签也不需要经过后端，直接前端展示）所有操作点玩完成后，所选标签添加到数据库中书本信息标签字段。数组字段
     * */

    /**
    * 添加二级标签
    * */
    @Override
    public BackResponseEntity<Object> insertTwoLabel(AInsertLabelReq aInsertLabelReq){
        String labelname = null;

        if(!ObjectUtils.isEmpty(aInsertLabelReq.getLabelName())){
            labelname = aInsertLabelReq.getLabelName()
                    .trim();
        }

        if(ObjectUtils.isEmpty(labelname)){
            return BackResponseEntity.success(ErrCodeEnum.unknown_4006, "参数缺失");
        }

        LeveltwoLabel leveltwoLabel = new LeveltwoLabel();
        leveltwoLabel.setLabelName(labelname);

        int i = leveltwoLabelMapper.insert(leveltwoLabel);

        if(i > 0){
            return BackResponseEntity.success(ErrCodeEnum.unknown_0, "插入标签成功");
        }else {
            return BackResponseEntity.success(ErrCodeEnum.unknown_1009, "插入标签失败");
        }
    }

    /**
    推荐二级标签
     在推荐标签页面展示，作者可选，确定后标签会存入书籍基本信息表
    */
    @Override
    public BackResponseEntity<ListResult<List<AQueryLabelRes>>> queryTwoLabel(){
        List<AQueryLabelRes> list = aLabelMapper.queryTwoLabel();

        ListResult<List<AQueryLabelRes>> listResult = new ListResult(10,list);

        return BackResponseEntity.success(ErrCodeEnum.unknown_0, listResult, "");

    }

    /**
     * 添加一级标签
     * */
    @Override
    public BackResponseEntity<Object> insertOneLabel(AInsertLabelReq aInsertLabelReq){
        String labelname = null;

        if(!ObjectUtils.isEmpty(aInsertLabelReq.getLabelName())){
            labelname = aInsertLabelReq.getLabelName()
                    .trim();
        }

        if(ObjectUtils.isEmpty(labelname)){
            return BackResponseEntity.success(ErrCodeEnum.unknown_4006, "参数缺失");
        }

        LeveltwoLabel leveltwoLabel = new LeveltwoLabel();
        leveltwoLabel.setLabelName(labelname);

        int i = leveltwoLabelMapper.insert(leveltwoLabel);

        if(i > 0){
            return BackResponseEntity.success(ErrCodeEnum.unknown_0, "插入标签成功");
        }else {
            return BackResponseEntity.success(ErrCodeEnum.unknown_1009, "插入标签失败");
        }
    }

    /**
     推荐一级标签
     */
    @Override
    public BackResponseEntity<ListResult<List<AQueryLabelRes>>> queryOneLabel(){
        List<AQueryLabelRes> list = aLabelMapper.queryOneLabel();

        ListResult<List<AQueryLabelRes>> listResult = new ListResult(10,list);

        return BackResponseEntity.success(ErrCodeEnum.unknown_0, listResult, "");

    }
}
